package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.CurePigDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskCourseDAO;
import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.PigDAO;

import cn.edu.scau.cmi.hombio.domain.CurePig;
import cn.edu.scau.cmi.hombio.domain.CureTaskCourse;
import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Pig;

import cn.edu.scau.cmi.hombio.service.CurePigService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for CurePig entities
 * 
 */

@Controller("CurePigRestController")

public class CurePigRestController {

	/**
	 * DAO injected by Spring that manages CurePig entities
	 * 
	 */
	@Autowired
	private CurePigDAO curePigDAO;

	/**
	 * DAO injected by Spring that manages CureTaskCourse entities
	 * 
	 */
	@Autowired
	private CureTaskCourseDAO cureTaskCourseDAO;

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for CurePig entities
	 * 
	 */
	@Autowired
	private CurePigService curePigService;

	/**
	 * Save an existing CureTaskCourse entity
	 * 
	 */
	@RequestMapping(value = "/CurePig/{curepig_id}/cureTaskCourse", method = RequestMethod.PUT)
	@ResponseBody
	public CureTaskCourse saveCurePigCureTaskCourse(@PathVariable Integer curepig_id, @RequestBody CureTaskCourse curetaskcourse) {
		curePigService.saveCurePigCureTaskCourse(curepig_id, curetaskcourse);
		return cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse.getId());
	}

	/**
	* Save an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/employee", method = RequestMethod.PUT)
	@ResponseBody
	public Employee saveCurePigEmployee(@PathVariable Integer curepig_id, @RequestBody Employee employee) {
		curePigService.saveCurePigEmployee(curepig_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Show all CurePig entities
	* 
	*/
	@RequestMapping(value = "/CurePig", method = RequestMethod.GET)
	@ResponseBody
	public List<CurePig> listCurePigs() {
		return new java.util.ArrayList<CurePig>(curePigService.loadCurePigs());
	}

	/**
	* View an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/employee/{employee_id}", method = RequestMethod.GET)
	@ResponseBody
	public Employee loadCurePigEmployee(@PathVariable Integer curepig_id, @PathVariable Integer related_employee_id) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		return employee;
	}

	/**
	* Delete an existing CurePig entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCurePig(@PathVariable Integer curepig_id) {
		CurePig curepig = curePigDAO.findCurePigByPrimaryKey(curepig_id);
		curePigService.deleteCurePig(curepig);
	}

	/**
	* Select an existing CurePig entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}", method = RequestMethod.GET)
	@ResponseBody
	public CurePig loadCurePig(@PathVariable Integer curepig_id) {
		return curePigDAO.findCurePigByPrimaryKey(curepig_id);
	}

	/**
	* View an existing CureTaskCourse entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/cureTaskCourse/{curetaskcourse_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureTaskCourse loadCurePigCureTaskCourse(@PathVariable Integer curepig_id, @PathVariable Integer related_curetaskcourse_id) {
		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourse_id, -1, -1);

		return curetaskcourse;
	}

	/**
	* Get Employee entity by CurePig
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/employee", method = RequestMethod.GET)
	@ResponseBody
	public Employee getCurePigEmployee(@PathVariable Integer curepig_id) {
		return curePigDAO.findCurePigByPrimaryKey(curepig_id).getEmployee();
	}

	/**
	* Create a new CureTaskCourse entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/cureTaskCourse", method = RequestMethod.POST)
	@ResponseBody
	public CureTaskCourse newCurePigCureTaskCourse(@PathVariable Integer curepig_id, @RequestBody CureTaskCourse curetaskcourse) {
		curePigService.saveCurePigCureTaskCourse(curepig_id, curetaskcourse);
		return cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse.getId());
	}

	/**
	* Save an existing CurePig entity
	* 
	*/
	@RequestMapping(value = "/CurePig", method = RequestMethod.PUT)
	@ResponseBody
	public CurePig saveCurePig(@RequestBody CurePig curepig) {
		curePigService.saveCurePig(curepig);
		return curePigDAO.findCurePigByPrimaryKey(curepig.getId());
	}

	/**
	* Delete an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/pig/{pig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCurePigPig(@PathVariable Integer curepig_id, @PathVariable Integer related_pig_id) {
		curePigService.deleteCurePigPig(curepig_id, related_pig_id);
	}

	/**
	* Save an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/pig", method = RequestMethod.PUT)
	@ResponseBody
	public Pig saveCurePigPig(@PathVariable Integer curepig_id, @RequestBody Pig pig) {
		curePigService.saveCurePigPig(curepig_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* Get CureTaskCourse entity by CurePig
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/cureTaskCourse", method = RequestMethod.GET)
	@ResponseBody
	public CureTaskCourse getCurePigCureTaskCourse(@PathVariable Integer curepig_id) {
		return curePigDAO.findCurePigByPrimaryKey(curepig_id).getCureTaskCourse();
	}

	/**
	* Create a new Pig entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/pig", method = RequestMethod.POST)
	@ResponseBody
	public Pig newCurePigPig(@PathVariable Integer curepig_id, @RequestBody Pig pig) {
		curePigService.saveCurePigPig(curepig_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* Create a new Employee entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/employee", method = RequestMethod.POST)
	@ResponseBody
	public Employee newCurePigEmployee(@PathVariable Integer curepig_id, @RequestBody Employee employee) {
		curePigService.saveCurePigEmployee(curepig_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* View an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/pig/{pig_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pig loadCurePigPig(@PathVariable Integer curepig_id, @PathVariable Integer related_pig_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		return pig;
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Get Pig entity by CurePig
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/pig", method = RequestMethod.GET)
	@ResponseBody
	public Pig getCurePigPig(@PathVariable Integer curepig_id) {
		return curePigDAO.findCurePigByPrimaryKey(curepig_id).getPig();
	}

	/**
	* Delete an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/employee/{employee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCurePigEmployee(@PathVariable Integer curepig_id, @PathVariable Integer related_employee_id) {
		curePigService.deleteCurePigEmployee(curepig_id, related_employee_id);
	}

	/**
	* Delete an existing CureTaskCourse entity
	* 
	*/
	@RequestMapping(value = "/CurePig/{curepig_id}/cureTaskCourse/{curetaskcourse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCurePigCureTaskCourse(@PathVariable Integer curepig_id, @PathVariable Integer related_curetaskcourse_id) {
		curePigService.deleteCurePigCureTaskCourse(curepig_id, related_curetaskcourse_id);
	}

	/**
	* Create a new CurePig entity
	* 
	*/
	@RequestMapping(value = "/CurePig", method = RequestMethod.POST)
	@ResponseBody
	public CurePig newCurePig(@RequestBody CurePig curepig) {
		curePigService.saveCurePig(curepig);
		return curePigDAO.findCurePigByPrimaryKey(curepig.getId());
	}
}